package com.xkdx.caipiao.presistence.historyquery;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xkdx.caipiao.HomePageActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.hongbao.HongbaoInfo;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class HistoryQueryModule1 extends AbsModule {
	private static String TAG = "log";
	public ArrayList<HongbaoInfo> list1;
	public List<HistoryQueryInfo> list;
	public LoginInfo loginInfo;
	public String message;
	public String code;
	private Context context;

	public HistoryQueryModule1(Context cx) {
		this.context = cx;
	}

	@Override
	public void parseData() throws Exception {
		try {
			backJson = (JSONArray) result;
			// System.err.println(backJson);
			for (int i = 0; i < backJson.size(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("HistoryQuery")) {
//					parseHistoryQuery(jsonObj.getJSONArray("DetailInfo"));
				} else if (sortOfAct.equals("AutoLogin")) {
					message = jsonObj.getString("Message");
					code = jsonObj.getString("Code");
					if(code.equals("5004")) {
						return;
					}
					parseLogin(jsonObj.getJSONObject("DetailInfo"));
				} else if (sortOfAct.equals("GetRedPacketUseList")) {
//					parseHongbao(jsonObj.getJSONArray("DetailInfo"));
				}

			}
		} catch (Exception e) {
			// to do define a exception include all parseException
			throw e;
		}

	}

	private void parseLogin(JSONObject person) throws Exception {
		try {

            UserSharePrefence1 usrSP = new UserSharePrefence1(context, IConstants.SP_USER_NEW);
            usrSP.setMoney(person.getString(LoginInfo.MONEY));
//            usrSP.setRedpacket(person.getString(LoginInfo.REDBOX));
            usrSP.setScore(person.getString(LoginInfo.SCORE));

//			loginInfo = new LoginInfo();
//			loginInfo.setCreate_time(person.getString(LoginInfo.CREATE_TIME));
//			loginInfo.setId(person.getString(LoginInfo.ID));
//			loginInfo.setDevicetype((person.getString(LoginInfo.DEVICETYPE)));
//			loginInfo.setLast_login_ip(person
//					.getString(LoginInfo.LAST_LOGIN_IP));
//			loginInfo.setLast_login_time(person
//					.getString(LoginInfo.LAST_LOGIN_TIME));
//			loginInfo.setLevel(person.getString(LoginInfo.LEVEL));
//			Log.i(TAG, person.getString(LoginInfo.LOGINTOKEN));
//
//			loginInfo.setLogintoken(person.getString(LoginInfo.LOGINTOKEN));
//
//			loginInfo.setMoney(person.getString(LoginInfo.MONEY));
//			loginInfo.setRedbox(person.getString(LoginInfo.REDBOX));
//			loginInfo.setScore(person.getString(LoginInfo.SCORE));
//			loginInfo.setSign_words(person.getString(LoginInfo.SIGN_WORDS));
//			loginInfo.setStatus(person.getString(LoginInfo.STATUS));
//			loginInfo.setUser_login_name(person
//					.getString(LoginInfo.USER_LOGIN_NAME));
//			loginInfo.setUser_nickname(person
//					.getString(LoginInfo.USER_NICKNAME));
//			loginInfo.setUser_pass(person.getString(LoginInfo.USER_PASS));
//			loginInfo.setUser_pic_assetid(person
//					.getString(LoginInfo.USER_PIC_ASSETID));
//			loginInfo.setVerify(person.getString(LoginInfo.VERIFY));
//
////			HomePageActivity.loginInfo = loginInfo;
//			SharePrefenceUtil su = new SharePrefenceUtil(context,
//					SharePrefenceUtil.USERINFO);
//			su.saveObject(SharePrefenceUtil.USERINFO, loginInfo);

		} catch (Exception e) {

			throw e;
		}
	}

	private void parseHistoryQuery(JSONArray person) throws Exception {
		try {
			list = new ArrayList<HistoryQueryInfo>();

			for (int i = 0; i < person.size(); i++) {
				JSONObject obj = person.getJSONObject(i);
				HistoryQueryInfo info = new HistoryQueryInfo();
				info.setBacktime(obj.getString(HistoryQueryInfo.BACKTIME));
				info.setChannelid(obj.getString(HistoryQueryInfo.CHANNELID));
				info.setCtime(obj.getString(HistoryQueryInfo.CTIME));
				info.setDevicetype(obj.getString(HistoryQueryInfo.DEVICETYPE));
				info.setDrawback(obj.getString(HistoryQueryInfo.DRAWBACK));
				info.setId(obj.getString(HistoryQueryInfo.ID));
				info.setIspay(obj.getString(HistoryQueryInfo.ISPAY));
				info.setIssueid(obj.getString(HistoryQueryInfo.ISSUEID));
				info.setLotteryname(obj.getString(HistoryQueryInfo.LOTTERYNAME));
				info.setOrderid(obj.getString(HistoryQueryInfo.ORDERID));
				info.setOrderpaymoney(obj
						.getString(HistoryQueryInfo.ORDERPAYMONEY));
				info.setOrdertime(obj.getString(HistoryQueryInfo.ORDERTIME));
				info.setPtime(obj.getString(HistoryQueryInfo.PTIME));
				info.setStatus(obj.getString(HistoryQueryInfo.STATUS));
				info.setTradestatus(obj.getString(HistoryQueryInfo.TRADESTATUS));
				info.setUid(obj.getString(HistoryQueryInfo.UID));

				info.setOuttime(obj.getString(HistoryQueryInfo.OUTTIME));
				info.setOrderpayred(obj.getString(HistoryQueryInfo.ORDERPAYRED));

				info.setIssurepay(obj.getString(HistoryQueryInfo.ISSURPAY));

				list.add(info);

			}

		} catch (Exception e) {

			throw e;
		}

	}

	private void parseHongbao(JSONArray jsonArray) throws Exception {
		try {
			list1 = new ArrayList<HongbaoInfo>();
			JSONArray array = jsonArray;
			for (int i = 0; i < array.size(); i++) {

				JSONObject obj = array.getJSONObject(i);
				HongbaoInfo hongbaoInfo = new HongbaoInfo();
				hongbaoInfo.setId(obj.getString("id"));
				hongbaoInfo.setPacketid(obj.getString("packetid"));
				hongbaoInfo.setCtime(obj.getString("ctime"));
				hongbaoInfo.setActtype(obj.getString("acttype"));
				hongbaoInfo.setUid(obj.getString("uid"));
				hongbaoInfo.setOid(obj.getString("oid"));
				hongbaoInfo.setStatus(obj.getString("status"));
				hongbaoInfo.setOrderid(obj.getString("orderid"));
				hongbaoInfo.setDetail(obj.getString("detail"));
				hongbaoInfo.setFacevalue(obj.getString("facevalue"));
				hongbaoInfo.setValidity(obj.getString("validity"));

				list1.add(hongbaoInfo);

			}

		} catch (Exception e) {

			throw e;
		}
	}
}
